//
//  NYAirlineProblemCell.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/8.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYAirlineProblemCell.h"

#import "NYAirlineItemModel.h"

@interface NYAirlineProblemCell ()

@property (nonatomic, strong) UIImageView *problemHeadImgView;
@property (nonatomic, strong) UILabel *problemNickLabel;
@property (nonatomic, strong) UILabel *problemTimeLabel;
@property (nonatomic, strong) UILabel *problemContentLabel;

@property (nonatomic, strong) UIView *answerView;

@property (nonatomic, strong) UIImageView *answerHeadImgView;
@property (nonatomic, strong) UILabel *answerNickLabel;
@property (nonatomic, strong) UILabel *answerTimeLabel;
@property (nonatomic, strong) UILabel *answerContentLabel;


@property (nonatomic, strong) UILabel *waitLabel;
@property (nonatomic, strong) UIImageView *waitImgView;

@end

@implementation NYAirlineProblemCell

//MARK :Nimbus
- (BOOL)shouldUpdateCellWithObject:(NYAirlineItemModel *)object {
    
    if (![object isKindOfClass:[NYAirlineItemModel class]]) {
        return false;
    }
    [self updateContentWithModel:object];
    return true;
}
//+ (CGFloat)heightForObject:(id)object identifier:(NSString *)identifier atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
//
//    return 105.f;
//}
- (void)setupView {
    [super setupView];
    
    //布局
    self.accessoryType = UITableViewCellAccessoryNone;
    [self.contentView addSubview:self.problemHeadImgView];
    [self.contentView addSubview:self.problemNickLabel];
    [self.contentView addSubview:self.problemTimeLabel];
    [self.contentView addSubview:self.problemContentLabel];
    [self.contentView addSubview:self.answerView];
    
    [_answerView addSubview:self.answerHeadImgView];
    [_answerView addSubview:self.answerNickLabel];
    [_answerView addSubview:self.answerTimeLabel];
    [_answerView addSubview:self.answerContentLabel];

    [_answerView addSubview:self.waitLabel];
    [_answerView addSubview:self.waitImgView];

    
    [self.problemHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(self.contentView).offset(15);
        make.height.width.mas_equalTo(40);
    }];
    [self.problemNickLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.problemHeadImgView.mas_right).offset(10);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.contentView).offset(18);
        make.height.mas_equalTo(16);
    }];
    [self.problemTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.problemNickLabel);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.problemNickLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(13);
    }];
    
    [self.problemContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.problemHeadImgView.mas_bottom).offset(14);
        make.bottom.equalTo(self.answerView.mas_top).offset(-15).priorityHigh();
    }];
    
    
    
    [self.answerHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.top.equalTo(self.answerView).offset(10);
        make.width.height.mas_equalTo(33);
    }];

    [self.answerNickLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.answerHeadImgView.mas_right).offset(8);
        make.right.equalTo(self.answerView).offset(-15);
        make.top.equalTo(self.answerView).offset(13);
        make.height.mas_equalTo(13);
    }];
    [self.answerTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.answerNickLabel);
        make.right.equalTo(self.answerView).offset(-15);
        make.top.equalTo(self.answerNickLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(13);
    }];
    [self.answerContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.answerView).offset(15);
        make.right.equalTo(self.answerView).offset(-15);
        make.top.equalTo(self.answerHeadImgView.mas_bottom).offset(9);
        make.bottom.equalTo(self.answerView.mas_bottom).offset(-14);
    }];

    [self.answerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.problemContentLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
//        make.height.mas_equalTo(40);
    }];

    
    [self.waitImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.answerView.mas_left).offset(__WGWidth(139));
        make.top.equalTo(self.answerView.mas_top).offset(13);
        make.width.height.mas_equalTo(15);
    }];
    [self.waitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.waitImgView.mas_right).offset(6);
        make.right.equalTo(self.answerView).offset(-15);
        make.top.bottom.equalTo(self.answerView);
    }];
}
- (void)updateContent {
    [super updateContent];
}
#pragma mark - Target Methods

#pragma mark - Public Methods

#pragma mark - Private Method

- (void)updateContentWithModel:(NYAirlineItemModel *)model {
    
    [self.problemHeadImgView sd_setImageWithURL:[NSURL URLWithString:model.problem_avatar] placeholderImage:[UIImage xuan_ke_wen_mo_ren]];
    self.problemNickLabel.text = model.problem_username;
    self.problemTimeLabel.text = [AppUnit formatGMT8TimeMillisecond:model.problem_posttime*1000 withFormat:@"yyyy-MM-dd HH:mm:ss"];//@"2017-04-23 17:56:23";
    self.problemContentLabel.text = [[NSAttributedString alloc] initWithHTMLData:[NOT_NULL_STRING_DEFAULT_EMPTY(model.problem_content) dataUsingEncoding:NSUnicodeStringEncoding] documentAttributes:nil].string;
    
    if (model.status == 1) { //未处理

        self.waitImgView.image = [UIImage deng_dai];
        self.waitLabel.text = @"等待老师处理";
        
        [self.answerView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.problemContentLabel.mas_bottom).offset(10);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
            make.height.mas_equalTo(40);
        }];
        self.answerHeadImgView.hidden = true;
        self.answerNickLabel.hidden = true;
        self.answerTimeLabel.hidden = true;
        self.answerContentLabel.hidden = true;

    } else {
        
        [self.answerHeadImgView sd_setImageWithURL:[NSURL URLWithString:model.answer_teacher_avatar]  placeholderImage:[UIImage xuan_ke_wen_da_lao_shi]];
        self.answerNickLabel.text = model.answer_teacher_nicename;
        self.answerTimeLabel.text = [self TimeStamp:model.answer_posttime];
        self.answerContentLabel.text = [[NSAttributedString alloc] initWithHTMLData:[NOT_NULL_STRING_DEFAULT_EMPTY(model.answer_content) dataUsingEncoding:NSUnicodeStringEncoding] documentAttributes:nil].string;
        
//        self.answerHeadImgView.hidden = false;
//        self.answerNickLabel.hidden = false;
//        self.answerTimeLabel.hidden = false;
//        self.answerContentLabel.hidden = false;

    }
}
-(NSString *)TimeStamp:(NSUInteger )timeStamp {
    
//    NSString *timeString = [NSString stringWithFormat:@"%ld",timeStamp];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *detaildate= [NSDate dateWithTimeIntervalSince1970:timeStamp];
    NSString *currentDateStr = [dateFormatter stringFromDate:detaildate];
    return currentDateStr;
}
#pragma mark - External Delegate

#pragma mark - Setter Getter Methods
- (UIImageView *)problemHeadImgView {
    if (!_problemHeadImgView) {
        _problemHeadImgView = [[UIImageView alloc] init];
        _problemHeadImgView.image = [UIImage imageWithColor:UIColorMakeWithHex(@"#E5E5E5")];
        _problemHeadImgView.layer.masksToBounds = true;
        _problemHeadImgView.layer.cornerRadius = 20.f;
    }
    return _problemHeadImgView;
}
- (UILabel *)problemNickLabel {
    if (!_problemNickLabel) {
        _problemNickLabel = [[UILabel alloc] init];
        _problemNickLabel.font = UIFontMake(15.f);
        _problemNickLabel.textColor = UIColorMakeWithHex(@"#333333");
    }
    return _problemNickLabel;
}
- (UILabel *)problemTimeLabel {
    if (!_problemTimeLabel) {
        _problemTimeLabel = [[UILabel alloc] init];
        _problemTimeLabel.font = UIFontMake(12.f);
        _problemTimeLabel.textColor = UIColorMakeWithHex(@"#999999");
    }
    return _problemTimeLabel;
}
- (UILabel *)problemContentLabel {
    if (!_problemContentLabel) {
        _problemContentLabel = [[UILabel alloc] init];
        _problemContentLabel.font = UIFontMake(13.f);
        _problemContentLabel.textColor = UIColorMakeWithHex(@"#666666");
        _problemContentLabel.numberOfLines = 0;
    }
    return _problemContentLabel;
}
- (UIView *)answerView {
    if (!_answerView) {
        _answerView = [[UIView alloc] init];
        _answerView.backgroundColor = UIColorMakeWithHex(@"#F3F3F3");
        _answerView.layer.masksToBounds = true;
        _answerView.layer.cornerRadius = 3.f;
    }
    return _answerView;
}
- (UIImageView *)answerHeadImgView {
    if (!_answerHeadImgView) {
        _answerHeadImgView = [[UIImageView alloc] init];
        _answerHeadImgView.image = [UIImage imageWithColor:UIColorMakeWithHex(@"#E5E5E5")];
        _answerHeadImgView.layer.masksToBounds = true;
        _answerHeadImgView.layer.cornerRadius = 16.5f;
    }
    return _answerHeadImgView;
}
- (UILabel *)answerNickLabel {
    if (!_answerNickLabel) {
        _answerNickLabel = [[UILabel alloc] init];
        _answerNickLabel.font = UIFontMake(12.f);
        _answerNickLabel.textColor = UIColorMakeWithHex(@"#333333");
    }
    return _answerNickLabel;
}
- (UILabel *)answerTimeLabel {
    if (!_answerTimeLabel) {
        _answerTimeLabel = [[UILabel alloc] init];
        _answerTimeLabel.font = UIFontMake(11.f);
        _answerTimeLabel.textColor = UIColorMakeWithHex(@"#999999");
    }
    return _answerTimeLabel;
}
- (UILabel *)answerContentLabel {
    if (!_answerContentLabel) {
        _answerContentLabel = [[UILabel alloc] init];
        _answerContentLabel.font = UIFontMake(12.f);
        _answerContentLabel.textColor = UIColorMakeWithHex(@"#666666");
        _answerContentLabel.numberOfLines = 0;
    }
    return _answerContentLabel;
}
- (UIImageView *)waitImgView {
    if (!_waitImgView) {
        _waitImgView = [[UIImageView alloc] init];
    }
    return _waitImgView;
}
- (UILabel *)waitLabel {
    if (!_waitLabel) {
        _waitLabel = [[UILabel alloc] init];
        _waitLabel.font = UIFontMake(13.f);
        _waitLabel.textColor = UIColorMakeWithHex(@"#999999");
    }
    return _waitLabel;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
